package com.ty.cloudpicture.api.aliyun;


import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.ty.cloudpicture.api.aliyun.model.CreateOutPaintingTaskRequest;
import com.ty.cloudpicture.api.aliyun.model.CreateOutPaintingTaskResponse;
import com.ty.cloudpicture.api.aliyun.model.GetOutPaintingTaskResponse;
import com.ty.cloudpicture.common.ErrorCode;
import com.ty.cloudpicture.exception.BusinessException;
import com.ty.cloudpicture.exception.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ALiYunAiApi {
    @Value("${aliYunAi.apiKey}")
    private String apiKey;
    public static final String CREATE_OUT_PRINTING_TASK_URL= "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/out-painting";
    public static final String GET_OUT_PRINTING_TASK_URL = "https://dashscope.aliyuncs.com/api/v1/tasks/%s";

    public CreateOutPaintingTaskResponse createOutPaintingTask(CreateOutPaintingTaskRequest createOutPaintingTaskRequest) {
        ThrowUtils.throwIf(createOutPaintingTaskRequest == null, ErrorCode.PARAMS_ERROR);
        //创建请求
        HttpRequest request = HttpRequest.post(CREATE_OUT_PRINTING_TASK_URL)
                .header(Header.AUTHORIZATION, "xcljz " + apiKey)
                .header("X-DashScope-Async", "enable")
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
                .body(JSONUtil.toJsonStr(createOutPaintingTaskRequest));
        try(HttpResponse execute = request.execute()){
            if (!execute.isOk()){
                log.error("请求失败，错误码：{}，错误信息：{}",execute.getStatus(),execute.body());
                throw new BusinessException(ErrorCode.OPERATION_ERROR,"AI扩图失败");
            }
            CreateOutPaintingTaskResponse response = JSONUtil.toBean(execute.body(), CreateOutPaintingTaskResponse.class);
            String code = response.getCode();
            if (code != null){
                log.error("请求失败，错误码：{}，错误信息：{}",code,response.getMessage());
                throw new BusinessException(ErrorCode.OPERATION_ERROR,"AI扩图接口响应异常");
            }
            return response;
        }
    }
    //查询创建任务
    public GetOutPaintingTaskResponse getOutPaintingTask(String taskId){
        if (taskId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务id不能为空");
        }

        try (HttpResponse httpResponse = HttpRequest.get(String.format(GET_OUT_PRINTING_TASK_URL,taskId))
                .header(Header.AUTHORIZATION, "xcljz " + apiKey)
                .execute()){
            if (!httpResponse.isOk()){
                log.error("请求失败，错误码：{}，错误信息：{}",httpResponse.getStatus(),httpResponse.body());
                throw new BusinessException(ErrorCode.OPERATION_ERROR,"获取任务失败");
            }
            return JSONUtil.toBean(httpResponse.body(), GetOutPaintingTaskResponse.class);
        }
    }

}
